package com.mifengkong.frwidget.textview;

import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.util.AttributeSet;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by jack on 17/2/18
 */

public class TextViewUIComponent extends android.support.v7.widget.AppCompatTextView {
    public TextViewUIComponent(Context context) {
        super(context);
    }

    public TextViewUIComponent(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TextViewUIComponent(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setTextStyle(String str,int needChangeSize) {
        SpannableString ss = new SpannableString(str);
        ss.setSpan(new AbsoluteSizeSpan(needChangeSize, true), str.length() - 1, str.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        setText(ss);
    }

    public void setTextStyleLoanCommit(String str,int needChangeSize){
        SpannableString ss = new SpannableString(str);
        ss.setSpan(new AbsoluteSizeSpan(needChangeSize, true), str.length() - 1, str.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        ss.setSpan(new AbsoluteSizeSpan(needChangeSize, true), 0, 4, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        setText(ss);
    }

    public void setTextStyleMore(String str) {
        SpannableString ss = new SpannableString(str);
        int i = 0;
        Pattern p;
        p = Pattern.compile("[0-9]*");//在这里，编译 成一个正则。
        Matcher m;
        m = p.matcher(str);//获得匹配

        while (m.find()) { //注意这里，是while不是if
            String xxx = m.group();
            if (!TextUtils.isEmpty(xxx)) {
//                LogUtils.e(xxx + "---");
//                LogUtils.e(i + "///");
                ss.setSpan(new AbsoluteSizeSpan(30, true), i, i + xxx.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                i += xxx.length() - 1;
//                LogUtils.e(i+"++++");
            }
            i++;
        }
        setText(ss);
    }
}
